package deepbrain.simnetwork.monitor;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import deepbrain.simnetwork.task.SimulationTask;

public class WriteToFileMonitor extends MonitorAdapter<SimulationTask>
		implements SimTaskMonitor {

	private PrintWriter out;
	private int numOfSteps;

	public WriteToFileMonitor(String filePath) {
		try {
			out = new PrintWriter(new FileWriter(filePath));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public WriteToFileMonitor(boolean brief, String filePath) {
		this(filePath);
	}

	@Override
	public void onTaskStart(SimulationTask task) {
		numOfSteps = 0;
		if (out != null) {
			out.println("0.\t" + task.getNetworkState());
			out.flush();
		}
	}

	@Override
	public void onStepFinish(SimulationTask task) {
		numOfSteps++;
		if (out != null) {
			out.print(numOfSteps + ".\t");
			out.println(task.getNetworkState());
			out.flush();
		}
	}

	@Override
	public void onTaskFinish(SimulationTask task) {
		if (out != null) {
			out.println("\nFINISH\n");
			out.flush();
		}
	}

}
